# -*- coding: utf-8 -*-
# @Time : 2021/9/11 21:37
# @Author : 阿怪
# @Email : 673691277@qq.com
# @File : finance_page.py
# @Project : crm自动化测试项目实战
'''财务页面类'''

from selenium.webdriver.common.by import By
from page.base_page import BasePage
from time import sleep
import time
from selenium.webdriver.support.select import Select
today = time.strftime('%Y-%m-%d')  # 获取当前日期
from utils.util import HOST2
class FinancePage(BasePage):
    '''财务页面类'''



    #定义定位器
    url = '{}/index.php?m=finance'.format(HOST2)
    add_receivables_locator = (By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[3]/a[1]')  #【添加应收款】定位器
    receivables_name_locator = (By.ID,'name')  #应收款名定位器
    contract_name_loactor = (By.ID,'contract_name') #合同定位器
    contract_choice_locator = (By.XPATH,'//*[@id="data"]/tr[1]/td[1]/input')  #点击合同定位器
    contract_ok_locator = (By.XPATH,'/html/body/div[8]/div[3]/div/button[1]/span')  #点击OK
    price_locator = (By.XPATH,'//*[@id="price"]')   #收款金额定位器
    submit_locator = (By.NAME,'submit')  #【保存】定位器
    statistical_locator = (By.XPATH,'/html/body/div[5]/div[1]/ul/li[5]/a')  #【财务】定位器
    department_locator = (By.ID, 'department')  #部门定位器
    role_locator = (By.ID, 'role')  # 选择员工定位器
    start_time_locator = (By.ID, 'start_time')  # 开始时间定位器
    end_time_locator = (By.ID, 'end_time')  # 结束时间定位器
    search_locator = (By.XPATH, '//*[@id="searchForm"]/ul/li[4]/button')  # 【搜索】按钮定位器
    today = time.strftime('%Y-%m-%d')  # 获取当前日期
    payment_locator = (By.XPATH, '//*[@id="report_content"]/table/tbody/tr/td[5]/a')  #’应收金额‘定位器
    payment_click_locator = (By.XPATH, '/html/body/div[5]/div[1]/ul/li[1]/a') #【应收款】按钮定位器
    check_all_locator = (By.ID, 'check_all')  # 全选按钮定位器
    delete_locator = (By.ID, 'delete')  # 删除定位器

    #操作元素方法
    def add_receivables(self):
        '''添加应收款'''
        element = self.driver.find_element(*self.add_receivables_locator)
        element.click()


    def input_name(self, name):
        '''输入应收款名'''
        element = self.driver.find_element(*self.receivables_name_locator)
        element.send_keys(name)

    def click_contract(self):
        '''点击合同'''
        element1 = self.driver.find_element(*self.contract_name_loactor)
        element1.click()

    def choice_contract(self, choice=contract_choice_locator):
        '''选择合同'''
        element2 = self.driver.find_element(*choice)
        element2.click()
    def click_ok(self):
        '''点击OK'''
        self.driver.find_element(*self.contract_ok_locator).click()
        sleep(2)

    def clear_price(self):
        '''清空应收款金额'''
        element3 = self.driver.find_element(*self.price_locator)
        element3.clear()
    def input_price(self, money):
        '''输入应收款金额'''
        element3 = self.driver.find_element(*self.price_locator)
        element3.send_keys(money)

    def click_submit(self):
        '''点击保存'''
        element4 = self.driver.find_element(*self.submit_locator)
        element4.click()


    def edit_receivables(self,name,money,choice=contract_choice_locator):
        '''应收款编辑界面输入'''
        self.input_name(name)
        self.click_contract()
        self.choice_contract(choice)
        self.click_ok()
        self.clear_price()
        sleep(1)
        self.input_price(money)
        sleep(1)
        self.click_submit()
        sleep(2)



    def statistical_click(self):
        '''点击【财务】'''
        element = self.driver.find_element(*self.statistical_locator)
        element.click()

    def select_department(self, departmentname = '办公室'):
        '''选择部门'''
        select = Select(self.driver.find_element(*self.department_locator))
        select.select_by_visible_text(departmentname)

    def select_role(self, rolename= 'CEO-admin'):
        '''选择人员'''
        select2 = Select(self.driver.find_element(*self.role_locator))
        select2.select_by_visible_text(rolename)

    def input_start_time(self):
        '''选择开始时间'''
        self.driver.find_element(*self.start_time_locator).send_keys(today)

    def input_end_time(self):
        '''选择结束时间'''
        self.driver.find_element(*self.end_time_locator).send_keys(today)

    def click_search(self):
        '''点击搜索'''
        self.driver.find_element(*self.search_locator).click()  # 点击【搜索】

    def statistical_search(self,departmentname='办公室',rolename='CEO-admin' ):
        '''按条件搜索'''
        self.select_department(departmentname)
        self.select_role(rolename)
        sleep(1)
        self.input_start_time()
        self.input_end_time()
        self.click_search()
        sleep(2)



    def get_price(self):
        '''获取应收款金额'''
        payment_text = self.driver.find_element(*self.payment_locator).text
        return payment_text

    def click_payment(self):
        '''点击应收款'''
        element = self.driver.find_element(*self.payment_click_locator)
        element.click()

    def click_check_all(self):
        '''点击全选'''
        element1 = self.driver.find_element(*self.check_all_locator)
        element1.click()

    def click_delete(self):
        '''点击删除'''
        element2 = self.driver.find_element(*self.delete_locator)
        element2.click()

    def clear_payment(self):
        '''清除应收款数据'''
        self.click_payment()
        self.click_check_all()
        self.click_delete()
        self.alert()





